﻿using System;
using System.Collections;
using CfgMusic;
using com.m3sdk.common;
using CSUtils;
using G02;
using G02.SceneConfig;
using GlobalTripleJump;
using m3sdk;
using TripleJump;
using U3dGame;
using U3dUtils;
using UnityEngine;

namespace TripleJump {
    /// <summary>
    /// 喷气背包
    /// </summary>
    public class CJetpack {
        const string _tag = "CJetpack";
        public GameObject root, wing;
        private GameObject rocketEffect;
        ParticleSystem smokeEffect;
        Transform role;

        public void Init(Transform role) {
            this.role = role;
            var gg = C128_AssetsCore.GetModel(GlobalTripleJumpCfg.artPath_Jetpack, 1);
            this.root = gg;
            wing = root.transform.Find("wing").gameObject;
            rocketEffect = root.transform.Find("rocket_effect").gameObject;

            wing.SetActive(false);
            if (rocketEffect != null) {
                rocketEffect.SetActive(false);
                smokeEffect = rocketEffect.transform.Find("smoke_effect").GetComponent<ParticleSystem>();
            } else
                Debug.Log("Can not find rocket effect");
            gg.transform.SetParent(role.Find("cattle3/role/joint1/joint2/Jetpack"));
            gg.transform.localPosition = Vector3.zero;
            gg.transform.localEulerAngles = new Vector3(0, -90, 180);
        }

        float energyTotalDuration;

        float currentDuration;
        int roleId;

        public void SetEnergy(int roleId, float energyZDuration) {
            this.roleId = roleId;
            this.energyTotalDuration = energyZDuration;
            currentDuration = energyZDuration;
            _isUse = false;
            if (CPara.onEnergyChangeEvent != null) {
                CPara.onEnergyChangeEvent(roleId, energyZDuration, energyZDuration);
            }
        }

        public void SetMotionSpeed(int speed) {
            if (smokeEffect != null) {
                ParticleSystem.MainModule main = smokeEffect.main;
                main.maxParticles = speed * 5;
            }
        }

        public void IsShowWing(bool isShow) {
            wing.SetActive(isShow);
        }

        CMusicManage.CAudio startMusic;

        public void IsShowEffect(bool isShow) {
            o_updateId++;
            if (isShow) {
                if (currentDuration <= 0) {
                    return;
                }
                startMusic = CMusicManage.PlayMusic(MusicCfg.JetPackStart);

                CMonoMonitor.UpdateEventAdd(_tag, 0, "", I_Update);
                if (TripleJump_FristRoleFly.CPara.firstRoleId == roleId) {
                    CVibrator.OpenVibrator(0, 100);
                }
            } else {
                if (startMusic != null) {
                    startMusic.Stop();
                }
                if (currentDuration > 0) {
                    if (TripleJump_FristRoleFly.CPara.firstRoleId == roleId) {
                        CVibrator.OpenVibrator(0, 100);
                    }
                }
            }

            rocketEffect.SetActive(isShow);
            _isUse = isShow;
        }

        int o_updateId;
        float lastVibrateTime;
        const float vibrateTimeInterval = 0.3f;

        void I_Update(float zOpdateId) {
            currentDuration -= Time.deltaTime;
            if (currentDuration <= 0) {
                //effect.SetActive(false);
                //_isUse = false;
                IsShowEffect(false);
            }
            if (CPara.onEnergyChangeEvent != null) {
                CPara.onEnergyChangeEvent(roleId, currentDuration, energyTotalDuration);
            }
            if (TripleJump_FristRoleFly.CPara.firstRoleId == roleId) {
                if (lastVibrateTime + vibrateTimeInterval < Time.time) {
                    lastVibrateTime = Time.time;
                    CVibrator.OpenVibrator(0, 100);
                }
            }
        }

        public bool isUse { get { return _isUse; } }
        bool _isUse;
        public class CPara {
            /// <summary>
            /// 角色Id,当前能量，总能量
            /// </summary>
            public static Action<int, float, float> onEnergyChangeEvent;
        }
    }
}
